package cn.kgc.vue.service.impl;

import cn.kgc.vue.commons.QueryCondition;
import cn.kgc.vue.commons.ResponseResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.vue.entity.Template;
import cn.kgc.vue.service.TemplateService;
import cn.kgc.vue.mapper.TemplateMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;

/**
* @author mengshun
* @description 针对表【sys_template】的数据库操作Service实现
* @createDate 2024-07-11 13:59:28
*/
@Service
public class TemplateServiceImpl extends ServiceImpl<TemplateMapper, Template>implements TemplateService{


    @Override
    public ResponseResult templatePage(QueryCondition<Template> queryCondition) {

        Page<Template> templatePage = new Page<>(queryCondition.getPage(), queryCondition.getLimit());
        String fileName = queryCondition.getSearchParams().getFileName();
        LambdaQueryWrapper<Template> lambda = new QueryWrapper<Template>().lambda();
        lambda.eq(StringUtils.isNotBlank(fileName),Template::getFileName,fileName);

        baseMapper.selectPage(templatePage,lambda);

        return ResponseResult.success().data("total",templatePage.getTotal()).data("tableData",templatePage.getRecords());
    }

    @Transactional
    @Override
    public ResponseResult deleteTemplate(String id) {

        // 1. 删除本地资源

        File file = new File("D:/upload/springboot/" + id);
        if (file.exists()){
            file.delete();
        }
        // 2. 删除数据库信息
        LambdaQueryWrapper<Template> lambda = new QueryWrapper<Template>().lambda();
        lambda.eq(Template::getUuidName,id);

        baseMapper.delete(lambda);

        return ResponseResult.success().message("删除成功");
    }
}




